package com.peng.redis;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.peng.utils.JacksonUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

/**
 * JedisWithSetSample
 *
 * @author lupeng
 * Created on 2022-04-29
 */
public class JedisWithSetSample {

    private static final Logger logger = LoggerFactory.getLogger(JedisWithSetSample.class);

    private JedisPool pool;

    public static final String KEY = "setkey";
    public static final String KEY1 = "setkey1";

    @BeforeEach
    public void init() {
        pool = new JedisPool("localhost", 6379);
        logger.info("JedisPool create success");
    }

    @AfterEach
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }

    @Test
    public void sadd() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                jedis.sadd(KEY, String.valueOf(i));
            }
        }
    }

    @Test
    public void spop() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                logger.info("spop {}", jedis.spop(KEY));
            }
        }
    }

    @Test
    public void sismember() {
        try (Jedis jedis = pool.getResource()) {
            System.out.println(jedis.sismember(KEY, "9"));
        }
    }

    @Test
    public void srandmember() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                System.out.println(jedis.srandmember(KEY));
            }
        }
    }

    @Test
    public void srem() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 10; i++) {
                System.out.println(jedis.srem(KEY, "3"));
            }
        }
    }

    @Test
    public void smove() {
        try (Jedis jedis = pool.getResource()) {
            jedis.smove(KEY, KEY1, "6");
            System.out.println(jedis.smembers(KEY));
            System.out.println(jedis.smembers(KEY1));
        }
    }

    @Test
    public void scard() {
        try (Jedis jedis = pool.getResource()) {
            System.out.println(jedis.scard(KEY));
        }
    }

    @Test
    public void scan() {
        try (Jedis jedis = pool.getResource()) {
            //for (int i = 0; i < 9999; i++) {
            //    jedis.sadd(KEY, String.valueOf(i));
            //}

            ScanParams scanParams = new ScanParams().count(100).match("1*");
            String cursor = "";
            do {
                ScanResult<String> scanResult = jedis.sscan(KEY, cursor, scanParams);
                logger.info(JacksonUtil.toJson(scanResult.getResult()));
                cursor = scanResult.getCursor();
            } while (!StringUtils.equals(cursor, "0"));
        }
    }

    @Test
    public void sinter() {
        try (Jedis jedis = pool.getResource()) {
            jedis.del(KEY1, KEY);

            jedis.sadd(KEY, "1", "2", "3");
            jedis.sadd(KEY1, "3", "4", "5");


            Set<String> sinter = jedis.sinter(KEY, KEY1);
            logger.info(JacksonUtil.toJson(sinter));

            String newsetkey = "newsetkey";
            jedis.sinterstore(newsetkey, KEY, KEY1);
            logger.info(JacksonUtil.toJson(jedis.smembers(newsetkey)));
        }
    }

    @Test
    public void suinon() {
        try (Jedis jedis = pool.getResource()) {
            jedis.del(KEY1, KEY);

            jedis.sadd(KEY, "1", "2", "3");
            jedis.sadd(KEY1, "3", "4", "5");


            Set<String> sunion = jedis.sunion(KEY, KEY1);
            logger.info(JacksonUtil.toJson(sunion));
        }
    }

    @Test
    public void sdiff() {
        try (Jedis jedis = pool.getResource()) {
            jedis.del(KEY1, KEY);

            jedis.sadd(KEY, "1", "2", "3");
            jedis.sadd(KEY1, "3", "4", "5");


            Set<String> sunion = jedis.sdiff(KEY, KEY1);
            logger.info(JacksonUtil.toJson(sunion));
        }
    }
}
